package fr.utbm.gl52.smartgrid.view.mainpanel;

import java.awt.BorderLayout;
import java.awt.event.ActionListener;
import java.util.Map;
import java.util.UUID;

import javax.swing.JPanel;

import fr.utbm.gl52.smartgrid.util.BiControllable;
import fr.utbm.gl52.smartgrid.util.properties.Properties;
import fr.utbm.gl52.smartgrid.view.MainFrame;

public class MainPanel extends JPanel implements BiControllable {

	private MainFrame parentFrame;
	private PropertyEditorPanel propertyEditorPanel;
	private ActionListener externalController;
	private ActionListener internalController;

	private static final long serialVersionUID = 5130760823489871376L;
	// private WeakReference<PropertyEditorPanel> editPanel;
	private TypeSelectorPanel typeSelectorPanel;

	public MainPanel(MainFrame parentFrame) {
		this.setLayout(new BorderLayout());

		this.parentFrame = parentFrame;

		this.typeSelectorPanel = null;

		if (this.typeSelectorPanel != null)
			add(this.typeSelectorPanel, BorderLayout.NORTH);
	}

	@Override
	public void addController(ActionListener controller) {
		// TODO Auto-generated method stub
		this.typeSelectorPanel.addController(controller);

		if (this.propertyEditorPanel != null)
			this.propertyEditorPanel.addControllers(this.internalController,
					this.externalController);
	}

	@Override
	public void addControllers(ActionListener internalController,
			ActionListener externalController) {
		if (this.typeSelectorPanel != null)
			this.typeSelectorPanel.addController(internalController);

		if (this.propertyEditorPanel != null)
			this.propertyEditorPanel.addControllers(internalController,
					externalController);

		this.externalController = externalController;
		this.internalController = internalController;
	}

	/**
	 * Updates the TypeSelectorPanel
	 * 
	 * @param parentClass
	 */
	public void setTypeSelectorPanel(Class<?> parentClass) {

		// Remove old property editor panel
		removeOldPropertyEditorPanel();

		// Remove old type selector panel
		if (this.typeSelectorPanel != null)
			remove(this.typeSelectorPanel);

		this.typeSelectorPanel = new TypeSelectorPanel(parentClass);
		add(this.typeSelectorPanel, BorderLayout.NORTH);

		// Re-add controllers
		if (this.externalController != null && this.internalController != null)
			this.typeSelectorPanel.addController(this.externalController);

		invalidate();
		revalidate();
		repaint();

		// Repaint de la mainFrame
		this.parentFrame.invalidate();
		this.parentFrame.revalidate();
	}

	public PropertyEditorPanel getPropertyEditorPanel() {
		return this.propertyEditorPanel;
	}

	public void setPropertyEditorPanel(UUID uuid,
			Map<String, Properties<?>> properties) {

		if (this.propertyEditorPanel != null)
			remove(this.propertyEditorPanel);

		this.propertyEditorPanel = new PropertyEditorPanel(properties, uuid, true);

		this.propertyEditorPanel.addControllers(this.internalController,
				this.externalController);
		add(this.propertyEditorPanel, BorderLayout.CENTER);

		// invalidate();
		// revalidate();
		repaint();

		// Repaint de la mainFrame
		// parentFrame.invalidate();
		this.parentFrame.revalidate();

	}

	/**
	 * Removes the property editor panel if it exists
	 */
	public void removeOldPropertyEditorPanel() {
		if (this.propertyEditorPanel != null) {
			remove(this.propertyEditorPanel);

			// invalidate();
			// revalidate();
			repaint();
		}
	}

	public void hideTypeSelectorPanel() {
		remove(this.typeSelectorPanel);
	}
}
